<?php
Class Data_Schedule extends Data_Base{

    /**
	 * 表名
	 *
	 * @var string
	 */
    static $table = 'game_data';
    /**
	 * 主键
	 * 
	 * @var  int
	 */
    static $primaryKey = 'game_id';
    /**
	 * 软删除标记
	 *
	 * @var int
	 */
    static $delflag = 'is_del';

    /**
	 * 构造函数
	 * 
	 * @access public
	 * @return void
	 */
    public function __construct() {
        parent::__construct ();
        parent::$table = self::$table;
        parent::$primaryKey = self::$primaryKey;
    }
    //根据球队ID和比赛ID获取战术
    function getScheduleTactics($teamid,$gamecode){
        $sql = sprintf("select * FROM tactics WHERE gameid='%s' AND teamid = '%d' LIMIT 1",$gamecode,$teamid);
        $datas = $this->get_sql($sql);
        return $datas;
    }
    //检查球队ID和比赛ID
    function checkTeamGame($teamid,$gamecode){
        $sql = sprintf("select * FROM schedule WHERE gamecode='%s' AND (hteamid = '%d' OR vteamid = '%d') LIMIT 1",$gamecode,$teamid,$teamid);
        $datas = $this->get_sql($sql);
        return $datas;
    }
    /**
     * 根据条件获取赛程数据
     * */
    function getSchedule($filter = array(),$order = "datetime ASC",$limit = null){
        //白名单过滤
        $filter_array_type = array("season",'round','day','year','month','team','roundafter');
        $filter_array_type_useful = array("season",'round');
        //拼出WHERE条件
        $where = null;
        //指定赛季
        if(isset($filter['season'])) $where .= sprintf("`season` = '%04d' AND",intval($filter['season']));
        //指定轮次
        if(isset($filter['round'])) $where .= sprintf("`round`*1 = '%d' AND",intval($filter['round']));
        //指定该轮次之后
        if(isset($filter['roundafter'])) $where .= sprintf("`round`*1 >= '%d' AND",intval($filter['roundafter']));
        //如果指定球队
        if(isset($filter['team'])){
            if(is_numeric($filter['team']))
            $where .= sprintf("(`vteamid` = '%s' OR `hteamid` = '%s') AND",$filter['team'],$filter['team']);
            //如果指定了两个球队
            elseif (is_array($filter['team']) && count($filter['team']) == 2){
                list($team1,$team2) = $filter['team'];
                $where .= sprintf("((`vteamid` = '%s' AND `hteamid` = '%s') OR (`hteamid` = '%s' AND `vteamid` = '%s')) AND",$team1,$team2,$team2,$team1);
            }
        }
        //根据时间判断
        if(isset($filter['year']) && isset($filter['month'])){
            if(isset($filter['day'])){
                $where .= sprintf(" LEFT(`datetime`,10) = '%04d-%02d-%02d'",$filter['year'],$filter['month'],$filter['day']);
            }else{
                $where .= sprintf(" LEFT(`datetime`,7) = '%04d-%02d'",$filter['year'],$filter['month']);
            }
        }
        $where = trim($where,'AND');
        $where != null && $where = " WHERE ".$where;
        $order != null && $order = " ORDER BY ".$order;
        $limit != null && $limit = " LIMIT ".$limit;
        //拼接完成
        $sql = sprintf("select * from schedule %s %s %s",$where,$order,$limit);
//        exit();
        $datas = $this->get_sql($sql);
        return $datas;
    }

    //获得比赛战报
    function getAllDate($season){
        $sql	= sprintf("SELECT left(date,4) as year,substr(date,6,2) as month,substr(date,9,2) as day,weekday FROM `schedule` WHERE season='%d' AND date<>'' group by left(date,10) order by date ASC",$season);
        $datas = $this->get_sql($sql);
        return $datas;
    }
    //获得比赛战报
    function getGameRecord($season,$game_id){
        $sql	= sprintf("SELECT news_url FROM `Game_Data_Expand` WHERE season='%d' AND game_id='%s'",$season,$game_id);
        $datas = $this->get_sql($sql);
        return $datas;
    }
    //获得全部比赛数据
    function getAllGameData(){
        $sql	= "SELECT *,Game_Data.season as season,Game_Data.game_id as game_id FROM `Game_Data` left join `Game_Data_Expand` on `Game_Data`.`season` = `Game_Data_Expand`.`season` and `Game_Data`.`game_id` = `Game_Data_Expand`.`game_id`";
        $datas = $this->get_sql($sql);
        return $datas;
    }
    //获得全部比赛数据
    function getGameDataById($where_id = null,$season){
        if($where_id){
            $where = sprintf(" WHERE Game_Data.game_id IN (%s) AND Game_Data.`season` = '%s'",$where_id,$season);
        }
        $sql	= sprintf("SELECT Game_Data.game_id,Game_Data.round,Game_Data.season,Game_Data.game_number,Game_Data.game_date,Game_Data.h_team_name,Game_Data.v_team_name,Game_Data.h_total,Game_Data.v_total,Game_Data_Expand.news_url,Game_Data_Expand.poto_url,Game_Data_Expand.shilu_url,Game_Data_Expand.video_url FROM `Game_Data` left join `Game_Data_Expand` on `Game_Data`.`season` = `Game_Data_Expand`.`season` and `Game_Data`.`game_id` = `Game_Data_Expand`.`game_id` %s",$where);
        $datas = $this->get_sql($sql);
        return $datas;
    }
    //获取球队比分等信息
    function getScheduleScore($game_id,$season){
        $sql	= sprintf("SELECT h_total,v_total,v_team_name,h_team_name FROM `Game_Data` WHERE `game_id`='%s' AND `season`='%s'",$game_id,$season);
        $datas = $this->get_sql($sql);
        return $datas;
    }
}